﻿using AutoMapper;
using Modules;
using Modules.Project;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Entity;
using Modules.System.User;
using Modules.Common;

namespace ManagementApi.Models.Project;

/// <summary>
/// 收入信息扩展Dto
/// </summary>
public class ProjectGuaranteeIncomeDto : OperateDto
{
    /// <summary>
    ///
    ///</summary>
    public int Id { get; set; }

    /// <summary>
    /// 项目Id
    ///</summary>
    public int? ProjectId { get; set; }

    /// <summary>
    /// 保证金缴纳信息Id
    ///</summary>
    public int? GuaranteePaymentId { get; set; }

    /// <summary>
    /// 动用通知书文号
    /// </summary>
    public string? NotificationLetter { get; set; }

    /// <summary>
    /// 收入编号
    ///</summary>
    public string? Code { get; set; }

    /// <summary>
    /// 交易类型(100：资金入账 102：代发工资 103：计息 104：手续费 105：其他)
    ///</summary>
    public 交易类型? TradingType { get; set; }

    /// <summary>
    /// 交易类型
    /// </summary>
    public string? TradingTypeName { get; set; }

    /// <summary>
    /// 缴存类型（100：按预设比例缴存 101：免除保证金 102：减少保证金应交金额 103：缓缴保证金 104：增加保证金应缴金额）
    ///</summary>
    public 缴存类型? DepositeType { get; set; }

    /// <summary>
    /// 缴存类型
    /// </summary>
    public string? DepositeTypeName { get; set; }

    /// <summary>
    /// 收入方式(100：现金 101：银行保函 102：工程担保公司保函 103：工程保证保险 104：其它)
    ///</summary>
    public 收支方式? IncomeWay { get; set; }

    /// <summary>
    /// 收入方式
    /// </summary>
    public string? IncomeWayName { get; set; }

    /// <summary>
    /// 收入金额
    ///</summary>
    public decimal? Amount { get; set; }

    /// <summary>
    /// 收入日期
    ///</summary>
    public DateTime? PayDate { get; set; }

    /// <summary>
    /// 创建人
    ///</summary>
    public string? Creator { get; set; }

    /// <summary>
    /// 创建时间
    ///</summary>
    public DateTime? CreateTime { get; set; }

    /************* 额外显示字段 ****************/

    /// <summary>
    /// 项目名称
    /// </summary>
    public string? ProjectName { get; set; }

    /// <summary>
    /// 项目编号
    /// </summary>
    public string? ProjectNum { get; set; }

    /// <summary>
    /// 缴存账户名称
    /// </summary>
    public string? GuaranteeAccountName { get; set; }
}

/// <summary>
/// 收入信息扩展
/// </summary>
public static class ProjectGuaranteeIncomeExceptions
{
    /// <summary>
    /// 返回前端显示数据
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static ProjectGuaranteeIncomeDto AsDto(this ProjectGuaranteeIncome entity, IApplicationContext applicationContext)
    {
        ProjectGuaranteeIncomeDto projectGuaranteeIncomeDto = new();
        var dto = applicationContext.GetService<IMapper>().Map(entity, projectGuaranteeIncomeDto);
        var projectService = applicationContext.GetService<ProjectService>();
        var project = projectService.GetProjectInfo(entity.ProjectId);
        var projectGuaranteePaymentService = applicationContext.GetService<ProjectGuaranteePaymentService>();
        var projectGuaranteePayment = projectGuaranteePaymentService.GetProjectGuaranteePayment(entity.GuaranteePaymentId ?? 0);
        if (projectGuaranteePayment != null)
        {
            var projectGuaranteeAccountService = applicationContext.GetService<ProjectGuaranteeAccountService>();
            var projectGuaranteeAccount = projectGuaranteeAccountService.GetProjectGuaranteeAccount(projectGuaranteePayment?.GuaranteeAccountId ?? 0);
            dto.GuaranteeAccountName = projectGuaranteeAccount?.Name;
        }
        if (project != null)
        {
            #region 主管单位数据权限

            if (applicationContext.CurrentUser is IDeptUser deptUser)
            {
                if (deptUser.IsSuperAdministrator())
                    dto.HasEdit = true;
                else if (deptUser.IsRSAdministrator())
                {
                    if (deptUser.Level == AdministrativeLevel.City && project.CityCode == deptUser.CityCode)
                        dto.HasEdit = true;
                    if (deptUser.Level == AdministrativeLevel.Area && project.AreaCode == deptUser.AreaCode)
                        dto.HasEdit = true;
                    if (deptUser.Level == AdministrativeLevel.Province)
                        dto.HasEdit = true;
                }
                else dto.HasEdit = false;
            }

            #endregion 主管单位数据权限

            dto.ProjectName = project.Name;
            dto.ProjectNum = project.PrjNum;
        }
        dto.DepositeTypeName = dto.DepositeType?.GetDescription();
        dto.IncomeWayName = dto.IncomeWay?.GetDescription();
        dto.TradingTypeName = dto.TradingType?.GetDescription();

        return dto;
    }

    /// <summary>
    /// 转为导出数据
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public static IEnumerable<Dictionary<string, string>> AsExportDto(this IEnumerable<ProjectGuaranteeIncome> entitys)
    {
        List<Dictionary<string, string>> exportDatas = new List<Dictionary<string, string>>();
        var projectService = ServiceAccessor.GetService<ProjectService>();
        var projects = projectService.GetProjectInfoByProjectIds(entitys.Select(a => a.ProjectId));
        foreach (var item in entitys)
        {
            Dictionary<string, string> exportData = new Dictionary<string, string>();
            exportData["项目编号"] = projects.FirstOrDefault(a => a.ProjectId == item.ProjectId)?.PrjNum ?? string.Empty;
            exportData["项目名称"] = projects.FirstOrDefault(a => a.ProjectId == item.ProjectId)?.Name ?? string.Empty;
            exportData["收入编号"] = item.Code;
            exportData["交易类型"] = item.TradingType?.GetDescription() ?? string.Empty ?? string.Empty;
            exportData["缴存类型"] = item.DepositeType?.GetDescription() ?? string.Empty ?? string.Empty;
            exportData["收入方式"] = item.IncomeWay?.GetDescription() ?? string.Empty ?? string.Empty;
            exportData["收入金额"] = item.Amount?.ToString() ?? string.Empty;
            exportData["收入人"] = item.Creator;
            exportData["收入时间"] = item.PayDate?.ToString("yyyy-MM-dd") ?? string.Empty;
        }

        return exportDatas;
    }
}